//
//  CXHotCityLikeViewController.m
//  Travel
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "CXHotCityLikeViewController.h"
#import "CXHotCityLikeCollectionViewCell.h"
#import "CoreDataManager.h"
#import "Location.h"
#import "CXDestinationDetailViewController.h"

@interface CXHotCityLikeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>


@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArr;

@property (nonatomic, strong) CoreDataManager *manager;
@property (nonatomic, strong) UILabel *label_nil;

@end

@implementation CXHotCityLikeViewController

- (void)viewWillAppear:(BOOL)animated
{
    //self.dataArr = [NSMutableArray array];

    [self selectAllLike];
    
    [self.collectionView reloadData];
}

- (void)selectAllLike
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
 

    //排序 参数:根据什么排序 yes 升序  / no 降序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        
    }
    self.dataArr = [NSMutableArray arrayWithArray:fetchedObjects];
    if (self.dataArr.count == 0) {
        if (!self.label_nil) {
            self.label_nil = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 2 - 40 , HEIGHT / 2 - 40, WIDTH - 40, 60)];
            self.label_nil.text = @"暂无收藏";
            //  self.label.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
            [self.view addSubview:self.label_nil];
            self.navigationController.navigationBar.hidden = NO;
        }else{
            [self.label_nil removeFromSuperview];
        }

    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.manager = [CoreDataManager shareInstance];
    NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, HEIGHT / 3 );
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CXHotCityLikeCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"CXHot"];
    //self.collectionView.backgroundColor = [UIColor greenColor];
    

    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXHotCityLikeCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXHot" forIndexPath:indexPath];
    
    //item.backgroundColor = [UIColor redColor];
    item.model_location = self.dataArr[indexPath.item];

    return item;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    CXDestinationDetailViewController *detailVC = [[CXDestinationDetailViewController alloc] init];
    
    detailVC.model_data = self.dataArr[indexPath.row];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
